from utils.check_params_utlil import CheckParams
from utils.handler_util import HandlerUtil
from handlers.base_handler import BaseHandler
from auths.decorator import authenticated
from blls.rpc_blls.cache_bll import CacheBLL


class CreateCacheHandler(BaseHandler):
    # @authenticated
    def post(self):
        check_r = CheckParams(params=self.request_data,
                              conditions=[["content", True, ""]
                                          ]).check()
        if not check_r.is_success:
            return self.write(HandlerUtil.set_failure_response(message=check_r.message))
        content = check_r.data.get('content')
        res = CacheBLL(request=self).create_cache(content)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success, code=res.code))


class GetCacheHandler(BaseHandler):
    # @authenticated
    def post(self, cache_id):
        res = CacheBLL(request=self).get_cache(cache_id)
        return self.write(HandlerUtil.set_response(data=res.data, message=res.message,
                                                   is_success=res.is_success, code=res.code))
